{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.python基础试题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.1 python 中boolean、float 和 int 分别表示什么？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python中boolean、float和int分别是三种数值类型：\n",
    "（1）boolean表示布尔类型，用于逻辑判断，输出True或者False；因为bool为int的子类，所以用1表示True，0表示False。\n",
    "（2）float，浮点型，浮点数就是数学中的小数，浮点变量由尾数（包含数字的值）和指数（包含数字的数量级）表示。\n",
    "（3）int，整型，对应整数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2  设计求1-2+3-4+5 ... 99的所有数的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "cum = 0\n",
    "for i in range(1,100):\n",
    "    cum += (-1)**(i+1)*i\n",
    "print(cum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.3  将字符串 s=\"yoyo\" 转换成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['y', 'o', 'y', 'o']\n"
     ]
    }
   ],
   "source": [
    "s = \"yoyo\"\n",
    "print(list(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. python进阶基础试题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.1     for i in range(1,100)[2::3][-10:]: \n",
    "\n",
    "                print i \n",
    "                \n",
    "        理解这段代码，并说出它是如何取数的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72\n",
      "75\n",
      "78\n",
      "81\n",
      "84\n",
      "87\n",
      "90\n",
      "93\n",
      "96\n",
      "99\n"
     ]
    }
   ],
   "source": [
    "\"生成从1到99的整数，从第3个数开始（即数字3），往后每3个数提取一次，把提取出来的数组成新的队列，输出这个队列的后10个数\"\n",
    "for i in range(1,100)[2::3][-10:]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.2 使用init 实例化时自动运行  分别计算单只股票最高价和收盘价两个时间点差值问题,可统一为one、two两个时间点,其最高价和收盘价赋值为 one(15,7) two(66,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one_dif': 8}\n",
      "{'two_dif': 46}\n"
     ]
    }
   ],
   "source": [
    "class stock:\n",
    "    def __init__(self,time, high, close):\n",
    "        self.time=time\n",
    "        self.high = high\n",
    "        self.close = close\n",
    "    def calc(self):\n",
    "        return {self.time:self.high-self.close}\n",
    "one = stock('one_dif',15,7)\n",
    "two = stock('two_dif',66,20)\n",
    "print(one.calc())\n",
    "print(two.calc())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. pandas数据处理试题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.1 如何查看列名、怎么对数据转置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看列名：df.columns\n",
    "转置：df.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.2 读取data里的600029这只股票的DataFrame,将其收盘价转换成用Numpy的Array格式，并用talib计算10日EMA值，返回ndarray的最后五个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import talib as ta\n",
    "data = pd.read_excel('sz50.xlsx',sheetname = '600029.XSHG',index_col = 'datetime')\n",
    "close = data.close.values\n",
    "ema_10 = ta.EMA(close,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[ 15.08227205  15.2564044   15.44433088  15.72172526  15.96322976]\n"
     ]
    }
   ],
   "source": [
    "print(type(ema_10))\n",
    "print(ema_10[-5:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.3 读取sz50.xlsx的['600029.XSHG','600050.XSHG','601318.XSHG']的全数据做成Panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.panel.Panel'>\n",
      "Dimensions: 3 (items) x 215 (major_axis) x 5 (minor_axis)\n",
      "Items axis: 600029.XSHG to 601318.XSHG\n",
      "Major_axis axis: 2017-01-03 15:00:00 to 2017-11-20 15:00:00\n",
      "Minor_axis axis: close to volume\n"
     ]
    }
   ],
   "source": [
    "stocks = ['600029.XSHG','600050.XSHG','601318.XSHG']\n",
    "data_dict ={}\n",
    "for s in stocks:\n",
    "    data = pd.read_excel('sz50.xlsx',sheetname = s, index_col = 'datetime')\n",
    "    data_dict[s] = data\n",
    "PN = pd.Panel(data_dict)\n",
    "print(PN)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.4把Panel转成ndim为3的Numpy，然后用array的切片读取ndim为2的三只股票最近20天的收盘价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 215, 5)\n",
      "[[  13.85   13.82   13.83   14.11   14.25   14.39   15.1    15.15   15.22\n",
      "    14.97   14.89   14.99   14.71   15.07   15.35   16.     16.04   16.29\n",
      "    16.97   17.05]\n",
      " [   8.53    8.56    8.62    8.88    9.25    9.24    9.43    9.25    9.02\n",
      "     9.15    9.63    9.5     9.9     9.97    9.96    9.49    9.68    9.61\n",
      "     9.63    9.8 ]\n",
      " [ 134.83  134.56  137.5   143.18  143.48  144.15  143.61  143.65  144.35\n",
      "   143.36  146.41  144.71  148.56  156.53  157.12  157.72  154.99  163.52\n",
      "   168.58  169.57]]\n"
     ]
    }
   ],
   "source": [
    "data_array = np.array(PN).reshape(3,215,5)\n",
    "print(data_array.shape)\n",
    "print(data_array[0:,-20:,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.5 建立一个5*5的矩阵，值从0到24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]]\n"
     ]
    }
   ],
   "source": [
    "mat = np.mat(np.arange(25).reshape(5,5))\n",
    "print(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
